#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Módulos 
import sys, gtk, pygame, utils, objetos
from pygame.locals import *

# Constantes

# Clases
# ---------------------------------------------------------------------
        
class Linea:
	def __init__(self,texto,posx,posy):
		self.text,self.rect = utils.textotls(texto, posx,posy,14)
		
class Destino:
	def __init__(self,texto1,texto2,posx,posy,correcto):
		self.text1,self.rect = utils.textodream(texto1, posx,posy,20,(255,0,0))
		self.text2,self.rect2 = utils.textodream(texto2, posx,posy+20*utils.RES,20,(255,0,0))
		self.correcto = correcto
		self.completo = False
		if correcto:
			self.check_img = utils.load_image("images/check.png",True)
		else:
			self.check_img = utils.load_image("images/cross.png",True)	
		self.check_rect = self.check_img.get_rect()
		self.check_rect.centerx = posx+75*utils.RES
		self.check_rect.centery = posy-5*utils.RES
    

class Camion(pygame.sprite.Sprite):
    def __init__(self, x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = utils.load_image("images/c3_5_camion.png",True)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.x_ini = x
        self.y_ini = y
        self.sonido_choque = pygame.mixer.Sound("sounds/explosion.ogg")
        self.sonido_choque.set_volume(0.1)   
        self.sonido_ok = pygame.mixer.Sound("sounds/harp.ogg")
        self.sonido_ok.set_volume(0.3)   
        self.sonido_motor = pygame.mixer.Sound("sounds/camion.ogg")
        self.speed = 0.2
        
    def detectar_colision(self,arboles,destinos):
		for arbol in arboles:   
			if pygame.sprite.collide_rect(self, arbol):
				self.sonido_choque.play()
				self.rect.centerx=self.x_ini
				self.rect.centery=self.y_ini
		for destino in destinos:   		
			if pygame.sprite.collide_rect(self, destino):
				self.sonido_ok.play()
				self.rect.centerx=self.x_ini
				self.rect.centery=self.y_ini
				destino.completo = True
				
        
    def mover(self, time, keys):
		#self.sonido_motor.play()
		if self.rect.top >= 160*utils.RES:
			if keys[K_UP]:
				self.rect.centery -= self.speed * time
		if self.rect.bottom <= 460*utils.RES:
			if keys[K_DOWN]:
				self.rect.centery += self.speed * time    
		if self.rect.left >= 110*utils.RES:
			if keys[K_LEFT]:
				self.rect.centerx -= self.speed * time  
		if self.rect.right <= 690*utils.RES:
			if keys[K_RIGHT]:
				self.rect.centerx += self.speed * time
	
	


class Arbol(pygame.sprite.Sprite):
    def __init__(self, x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = utils.load_image("images/c3_5_arbol.png",True)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        
        

# ---------------------------------------------------------------------
def dibujar(screen,cursor_picture, clock):
    salir = 0
    background_image = utils.load_image('images/fondo_3.jpg')
    bosque = utils.load_image('images/c3_5_bosque.png')
    i= 0
    j= 0
    numero_linea = 1
    btn_adelante = objetos.BotonAdelante()
    btn_atras = objetos.BotonAtras()
    
    
    clase, clase_rect = utils.texto_clase("Clase 3", 740*utils.RES, 30*utils.RES)
           
    titulo, titulo_rect = utils.texto_subtitulo_azul("EL LABERINTO DE LA DISPOSICIÓN FINAL", 400*utils.RES, 70*utils.RES)
    linea1,linea1_rect = utils.textotls('Existen cuatro alternativas de DISPOSICIÓN FINAL de la basura. Conducí a los camiones para',400*utils.RES,100*utils.RES,16,(0,0,0))
    linea2,linea2_rect = utils.textotls('encontrarlas. ¡Ojo! Dos alternativas son falsas, así que ten cuidado por donde andas.', 400*utils.RES,120*utils.RES,16,(0,0,0))
    fin,fin_rect = utils.textotls('¡Muy bien! ¡Ahora ya sabemos cuales son verdaderas! ', 400*utils.RES,470*utils.RES,26,(255,0,0)) 
    camion1 = Camion(110*utils.RES,310*utils.RES)
    arboles = (Arbol(360*utils.RES,200*utils.RES), Arbol(400*utils.RES,360*utils.RES),Arbol(240*utils.RES,420*utils.RES),Arbol(480*utils.RES,270*utils.RES),Arbol(150*utils.RES,190*utils.RES),Arbol(250*utils.RES,300*utils.RES))
    destinos = (Destino('Vertederos','al aire libre',660*utils.RES,170*utils.RES,True),
    Destino('Relleno','al aire libre',660*utils.RES,220*utils.RES,False),
    Destino('Quema','a cielo abierto',660*utils.RES,270*utils.RES,True),
    Destino('Quema','de sanitario',660*utils.RES,320*utils.RES,False),
    Destino('Relleno','sanitario',660*utils.RES,370*utils.RES,True),
    Destino('Incineracion','',660*utils.RES,420*utils.RES,True))
    
    while salir == 0:
    
    	# Pump GTK messages.sub
		while gtk.events_pending():
			gtk.main_iteration()
		
		# Try to stay at 30 FPS
		time = clock.tick(30)
			
		keys = pygame.key.get_pressed()	
		for eventos in pygame.event.get():
			if eventos.type == QUIT:
				sys.exit(0)
			elif eventos.type == pygame.VIDEORESIZE:
			   pygame.display.set_mode(eventos.size, pygame.RESIZABLE)	
			elif eventos.type == MOUSEBUTTONDOWN:
				if btn_adelante.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 6
					btn_adelante.sonido.play()	
				elif btn_atras.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 4					
					btn_atras.sonido.play()	
				
		# verifica si hay colisiones		
		camion1.detectar_colision(arboles,destinos)
		final = True
		for destino in destinos:
			if destino.completo == False:
				final = False
			
		
		# Realiza la animacion del texto
		camion1.mover(time, keys)
		screen.blit(background_image, (0, 0))
		screen.blit(clase, clase_rect)
		#pygame.draw.rect(screen,(0,255,150),(110,150,525,300))
		screen.blit(bosque, (60*utils.RES, 140*utils.RES))
		screen.blit(titulo, titulo_rect)
		screen.blit(linea1,linea1_rect)
		screen.blit(linea2,linea2_rect)
		for arbol in arboles:
			screen.blit(arbol.image, arbol.rect)
		for destino in destinos:
			screen.blit(destino.text1, destino.rect)
			screen.blit(destino.text2, destino.rect2)
			if destino.completo:			
				screen.blit(destino.check_img, destino.check_rect)
		screen.blit(camion1.image, camion1.rect)
		if final:
			screen.blit(fin,fin_rect)
		screen.blit(btn_adelante.image, btn_adelante.rect)
		screen.blit(btn_atras.image, btn_atras.rect)
		screen.blit(cursor_picture, pygame.mouse.get_pos())
		pygame.display.flip()

		
                	 
    return salir

# This function is called when the game is run directly from the command line:
# ./TestGame.py 
def main():
	
	pygame.init()
	screen = pygame.display.set_mode((int(800*utils.RES), int(520*utils.RES)), pygame.RESIZABLE) #pygame.display.get_surface() #

	# Fake a Sugar cursor for the pyGame canvas area
	pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
	cursor_picture = pygame.image.load('images/standardcursor.png')
	cursor_picture.convert_alpha() 
	clock = pygame.time.Clock()
	dibujar(screen,cursor_picture,clock)

if __name__ == '__main__':
   main()
